﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CarLoanSystem.Entity;
using System.IO;
using System.Drawing.Imaging;

namespace CarLoanSystem
{
    public partial class frmEditCar : Form
    {
        private bool isLoaded;
        private CarInfoItem carItem;
        private static string filePath;
        private static string carPath;
        private static Image imageFile;
        private static OpenFileDialog fdlg;
        public frmEditCar()
        {
            InitializeComponent();
        }

        public frmEditCar(int id)
        {
            InitializeComponent();
            carItem = DAL.CarInfoDAL.LoadCar(id);
            setComboBox();
            this.cbbBrand.SelectedValue = carItem.CAR_BRAND_ID;
            this.cbbModel.SelectedValue = carItem.CAR_MODEL_ID;
            this.txtDetail.Text = carItem.CAR_DETAIL;
            this.txtPrice.Text = carItem.CAR_PRICE.ToString();
            filePath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.Parent.Parent.FullName + "\\CarLoanSystemWeb\\CarLoanSystemWeb";
            filePath += "\\Picture";
            this.txtTypeEngine.Text = carItem.CAR_TYPE_ENGINE;
            carPath = carItem.CAR_PATH_IMAGE;
            if (carItem.CAR_PATH_IMAGE != "")
            {
                imageFile = Image.FromFile(carPath);
                this.pictureBox.Image = imageFile;
                this.pictureBox.Refresh();
            }
        }

        private void setComboBox()
        {
            this.cbbBrand.DataSource = DAL.CarBrandInfoDAL.LoadAllForComboBox();
            this.cbbBrand.DisplayMember = "CAR_BRAND_NAME";
            this.cbbBrand.ValueMember = "CAR_BRAND_ID";

            this.cbbModel.DataSource = DAL.CarModelInfoDAL.LoadAllForComboBox(carItem.CAR_BRAND_ID);
            this.cbbModel.DisplayMember = "CAR_MODEL_NAME";
            this.cbbModel.ValueMember = "CAR_MODEL_ID";
        }

        private void btnCancle_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            try
            {
                carItem.CAR_DETAIL = this.txtDetail.Text;
                carItem.CAR_PRICE = Convert.ToDecimal(this.txtPrice.Text);
                carItem.CAR_TYPE_ENGINE = this.txtTypeEngine.Text;
                carItem.CAR_BRAND_ID = (int)this.cbbBrand.SelectedValue;
                carItem.CAR_BRAND_ID = (int)this.cbbModel.SelectedValue;

                if (txtPath.Text != "")
                {
                    string path = filePath + "\\" + carItem.CAR_BRAND_ID + "\\"
                        + carItem.CAR_MODEL_ID + "\\" + carItem.CAR_ID;
                    
                    Bitmap bmp = new Bitmap(txtPath.Text);
                    string[] pathArr = txtPath.Text.Split('\\');
                    string[] fileArr = pathArr.Last().Split('.');
                    string fileName = fileArr.First().ToString();
                    string fileLastName = fileArr.Last().ToString();
                   
                    carPath = path + "\\" + fileName + "." + fileLastName;

                    //string filePathForSave = carItem.CAR_BRAND_ID + "\\"
                    //    + carItem.CAR_MODEL_ID + "\\" + carItem.CAR_ID + "\\" + fileName + "." + fileLastName;

                    if (fdlg != null)
                    {
                        if (!Directory.Exists(path))
                            Directory.CreateDirectory(path);

                        try
                        {
                            File.Copy(fdlg.FileName, carPath, false);
                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show("กรุณาตั้งชื่อไฟล์ใหม่เนื่องจากชื่อไฟล์ซ้ำ", "พบข้อผิดพลาด");
                            return;
                        }
                    }
                    carItem.CAR_PATH_IMAGE = carPath;
                }
                DAL.CarInfoDAL.Update(carItem);
                MessageBox.Show("อัพเดทข้อมูลสำเร็จ", "ข้อมูลรถยนต์");
                this.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString(), "Error");
            }
        }

        private void btnBrowser_Click(object sender, EventArgs e)
        {
            fdlg = new OpenFileDialog();
            fdlg.Title = "C# Corner Open File Dialog";
            fdlg.InitialDirectory = @"Desktop\";
            fdlg.Filter = "All files (*.*)|*.*|All files (*.*)|*.*";
            fdlg.FilterIndex = 2;
            fdlg.RestoreDirectory = true;
            if (fdlg.ShowDialog() == DialogResult.OK)
            {
                txtPath.Text = fdlg.FileName;
                imageFile = null;
                imageFile = Image.FromFile(txtPath.Text);
                this.pictureBox.Image = imageFile;
                this.pictureBox.Refresh();
            }
        }

        private void cbbBrand_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (isLoaded)
            {
                this.cbbModel.DataSource = DAL.CarModelInfoDAL.LoadAllForComboBox(Convert.ToInt32(this.cbbBrand.SelectedValue));
            }
            else
            {
                isLoaded = true;
            }
        }
    }
}
